package com.niuxiaofa.newlaw.Activity;


import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Bean.BlindboxBuyInfoBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.TTAdManagerHolder;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.View.BlindBoxDialog;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 盲盒记录
 */
public class BlindBoxActivity extends BaseActivity {


    @BindView(R.id.primary)
    TextView primary;


    @BindView(R.id.intermediate)
    TextView intermediate;


    @BindView(R.id.advanced)
    TextView advanced;


    @BindView(R.id.mywarehouse)
    TextView mywarehouse;


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_blind_box;
    }

    @Override
    protected void initData() {

        setTitle("抽盲盒");
        primary.setOnClickListener(e -> {
            getblindboxBuyInfo("1");
        });

        intermediate.setOnClickListener(e -> {
            getblindboxBuyInfo("2");
        });

        advanced.setOnClickListener(e -> {
            getblindboxBuyInfo("3");
        });

        mywarehouse.setOnClickListener(e -> {

            ToActivityUtil.toNextActivity(MyWarehouseActivity.class);

        });





    }


    public void PlayAD() {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121593")
                .setRewardName("金币") //奖励的名称 选填
                .setRewardAmount(3)  //奖励的数量 选填
                //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景，只要设置的值大于0即可
                //且仅是模板渲染的代码位ID使用，非模板渲染代码位切勿使用
                .setExpressViewAcceptedSize(500, 500)
                .setUserID("tag123")//tag_id
                .setMediaExtra("media_extra") //附加参数
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        TTAdNative mTTAdNative = TTAdManagerHolder.get().createAdNative(getActivity());
        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(message));
                //   TToast.show(RewardVideoActivity.this, message);

            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {

            }

            //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
            @Override
            public void onRewardVideoCached() {
                Log.e(TAG, "Callback --> onRewardVideoCached");
                //  mIsLoaded = true;
                //     TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd video cached");
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ad) {
                Log.e(TAG, "Callback --> onRewardVideoCached");
                //  mIsLoaded = true;
                //    TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd video cached");
                ad.showRewardVideoAd(BlindBoxActivity.this, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");

                Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).openBlindbox(boxID);
                call.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        //判断请求是否成功
                        if (response.isSuccessful()) {
                            BaseBean bean = (BaseBean) response.body();

                            if (bean.getCode() == 1) {
                                BlindBoxDialog blindBoxDialog = new BlindBoxDialog(getActivity(), "确定要兑换" + title + "盲盒吗？", content, new BlindBoxDialog.Onclick() {
                                    @Override
                                    public void click() {
                                        PlayAD();
                                    }
                                });
                                blindBoxDialog.show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());

                    }
                });


            }
        });
    }

    String boxID;
    String title, content;

    public void getblindboxBuyInfo(String c) {


        Call<BlindboxBuyInfoBean> call = ServiceManager.getInstance().creat(ApiService.class).blindboxBuyInfo(c);
        call.enqueue(new Callback<BlindboxBuyInfoBean>() {
            @Override
            public void onResponse(Call<BlindboxBuyInfoBean> call, Response<BlindboxBuyInfoBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    BlindboxBuyInfoBean bean = (BlindboxBuyInfoBean) response.body();
                    content = "消耗" + bean.getData().getCurrntPrice() + "学分+ " + bean.getData().getCurrntSpend() + "能量";
                    boxID = bean.getData().getBoxid();
                    switch (c) {

                        case "1":
                            title = "初级";

                            break;

                        case "2":
                            title = "中级";

                            break;

                        case "3":
                            title = "高级";
                            break;

                    }

                    BlindBoxDialog blindBoxDialog = new BlindBoxDialog(getActivity(), "确定要兑换" + title + "盲盒吗？", content, new BlindBoxDialog.Onclick() {
                        @Override
                        public void click() {
                            PlayAD();
                        }
                    });
                    blindBoxDialog.show();
                }
            }

            @Override
            public void onFailure(Call<BlindboxBuyInfoBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());

            }
        });

    }
}
